﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZX.BLL.ClientMobi.Message;
using System.Data;

namespace ZX.WebApp.Extend
{
    public abstract class AB_DataHelp
    {
        public abstract string sqlParams { get; set; }

        protected abstract IMessagePly MsgPlayFrameWork {set;}

        public abstract object Execute();
        
    }

    public class DataHelpInstance:AB_DataHelp
    {


        private string _sqlParams;
        public override string sqlParams
        {
            get
            {
                return this._sqlParams;
            }
            set
            {
                this._sqlParams = value;
            }
        }

        private IMessagePly _MsgPlayFrameWork = null;
        protected override IMessagePly MsgPlayFrameWork
        {
            set { _MsgPlayFrameWork = value; }
        }

        protected IDictionary<string, object> sqlParamsContainer = new Dictionary<string, object>();

       

        protected virtual void SetSqlParams()
        {
            this.sqlParams = @"select ISNULL(y.cx,0) as cx,
z.UName as UserName,
ShortDeptName=dbo.fn_GetDeptName(p.DepartmentID, '2'),
zw= ISNULL((select PostName From DatPost where PostID= p.PostID), ''),
x.* From (select x.UserID From SysUserRole as x left join SysRole as y
on x.RoleID= y.RoleID where y.RoleName='月度建议显示权限') as x 
inner join [DatUser] as z on z.UserID=x.UserID
left join [hrEmployee] p on z.EmpID= p.ID
left join (
select COUNT(*) as cx, x.AddUser From [DatSysNewsMonth] as x inner join [DatSysNewsMonthInfo] as y 
on x.ID= y.NID
{0}
Group by x.AddUser
) as y on x.UserID= y.AddUser 
order by UserName asc";
        }


        public override object Execute()
        {
            MsgPlayFrameWork = new RoleBase().Instance();
            
            this.SetSqlParams();
            //Execute 
            return _MsgPlayFrameWork.GetListByTSQL(sqlParamsContainer);
        }

    }

    public class InitDataInstance : DataHelpInstance
    {
        protected override void SetSqlParams()
        {
            base.SetSqlParams();
            this.sqlParams = string.Format(sqlParams, " and DATEDIFF(m, x.createDate, GETDATE())=0");
            sqlParamsContainer.Add("t-sql", sqlParams);
        }  

    }

    public class SearchInstance : DataHelpInstance
    {
        string strDate1, strDate2;
        public SearchInstance(string d1, string d2)
        {
            strDate1 = d1;
            strDate2 = d2;
        }
        protected override void SetSqlParams()
        {
            base.SetSqlParams();
            string whereSqlParams = " and 1=1";
            if (!string.IsNullOrEmpty(strDate1))
            {
                DateTime dtBefore;
                DateTime.TryParse(strDate1, out dtBefore);
                whereSqlParams += " and x.createDate>='" + dtBefore + "'";
            }

            if (!string.IsNullOrEmpty(strDate2))
            {
                DateTime dtBefore2;
                DateTime.TryParse(strDate2, out dtBefore2);
                whereSqlParams += " and x.createDate<='" + dtBefore2 + "'";
            }



            this.sqlParams = string.Format(sqlParams, whereSqlParams);
            sqlParamsContainer.Add("t-sql", sqlParams);
        }

    }

    public partial class MsgreplySum : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitData();
            }

        }

        private void InitData()
        {

            AB_DataHelp help = new InitDataInstance();
            DataTable tempTab= help.Execute() as DataTable;
            if (tempTab != null && tempTab.Rows.Count > 0)
            {
                this.Repeater1.DataSource = tempTab;
                this.Repeater1.DataBind();
            }
            
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            
            AB_DataHelp help = new SearchInstance(this.TextBox1.Text.Trim(), this.TextBox2.Text.Trim());
            DataTable tempTab = help.Execute() as DataTable;
            if (tempTab != null && tempTab.Rows.Count > 0)
            {
                this.Repeater1.DataSource = tempTab;
                this.Repeater1.DataBind();
            }

        }
    }
}